package cn.sixos.action;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import cn.sixos.bo.LoginBo;
import cn.sixos.vo.UserVo;

@Controller
@SessionAttributes({ "userVo" })
@RequestMapping("/")
public class LoginAction {
	@Resource
	private LoginBo loginBo;
	
	@RequestMapping("gotoLogin")
	public String gotoLogin(){
		return "login";
	}
	@RequestMapping("login")
	public ModelAndView login(String userName, String password) {
		ModelAndView mav = new ModelAndView();
		// 进行登陆
		// 处理异常(登陆成功转到list，失败就发送msg，还是在login)
		try {
			// 因为类上有@SessionAttributes({"userVo"})
			// 所以通过以下语句可以把vo放到一个叫userVo的Session属性中
			UserVo vo = loginBo.doLogin(userName, password);
			mav.addObject("userVo", vo);
			if (vo.getRole().equals("A")) {
				mav.setViewName("redirect:admin/list.do");
			} else {
				mav.setViewName("redirect:student/list.do");
			}
			return mav;
		} catch (Exception e) {
			e.printStackTrace();
			mav.addObject("msg", e.getMessage());
			mav.setViewName("login");
			return mav;
		}

	}
}
